package com.west_traveler.find_page_fragment.cate_fragment;

import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.west_traveler.application.BaseApplication;
import com.west_traveler.entity.entity_category.entity_category_second.Entity_Cate_Fra_CategoryContents_Recom_Content;
import com.west_traveler.entity.entity_category.entity_category_second.Entity_Cate_single;
import com.west_traveler.inteface.HttpService;
import com.west_traveler.ximalayafm.R;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class Cate_SingleFragment extends Fragment implements Callback<Entity_Cate_single> {


    private String categoryId;
    private String tagName;
    private HttpService service;
    private int pageId = 1;
    private RecyclerAdapter_Cate_Single adapter;
    private int maxPage;
    private boolean loading;

    public static Cate_SingleFragment getInstance() {
        return new Cate_SingleFragment();
    }


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_cate_single,container,false);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        RecyclerView recycler = (RecyclerView) view.findViewById(R.id.recycler_frg_cate_single);
        Bundle arguments = getArguments();
        categoryId = arguments.getString("categoryId");
        tagName = arguments.getString("tagName");

        BaseApplication application = (BaseApplication) getActivity().getApplication();
        service = application.getService();
        service.getEntity_Cate_single("hot",categoryId,"android",pageId,20,0,tagName).enqueue(this);
        adapter = new RecyclerAdapter_Cate_Single(getContext());
        recycler.setAdapter(adapter);

        recycler.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                LinearLayoutManager layoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
                int totalItemCount = layoutManager.getItemCount();
                int lastVisibleItem = layoutManager.findLastVisibleItemPosition();
                if(!loading && totalItemCount==lastVisibleItem+1&&pageId<=maxPage){
                    loading=true;

                    service.getEntity_Cate_single("hot",categoryId,"android",++pageId,20,0,tagName).enqueue(new Callback<Entity_Cate_single>() {
                        @Override
                        public void onResponse(Call<Entity_Cate_single> call, Response<Entity_Cate_single> response) {
                            Entity_Cate_single body = response.body();;
                            List<Entity_Cate_Fra_CategoryContents_Recom_Content> datas = body.getList();
                            adapter.addAll(datas);
                            loading=false;
                        }

                        @Override
                        public void onFailure(Call<Entity_Cate_single> call, Throwable t) {
                            Toast.makeText(getContext(), "网络不好，single加载失败", Toast.LENGTH_SHORT).show();
                        }
                    });


                }



            }
        });
    }

    @Override
    public void onResponse(Call<Entity_Cate_single> call, Response<Entity_Cate_single> response) {
        Entity_Cate_single body = response.body();
        maxPage = body.getMaxPageId();
        List<Entity_Cate_Fra_CategoryContents_Recom_Content> datas = body.getList();
        adapter.addAll(datas);


    }

    @Override
    public void onFailure(Call<Entity_Cate_single> call, Throwable t) {
        Toast.makeText(getContext(), "网络不好，single加载失败", Toast.LENGTH_SHORT).show();
    }
}
